<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\Traits\Filters;

class RentClothAfter extends Model
{
    use Filters;

    protected $table = 'rent_cloth_after';

    public function user()
    {
        return $this->hasOne(User::class,'id','user_id');
    }

    public function rentcloth()
    {
        return $this->hasOne(RentCloth::class,'id','rent_id');
    }

    public function shop()
    {
        return $this->hasOne(Shop::class,'id','shop_id');
    }

    public function transform_buyAfter_list()
    {
        $attr_id = json_decode($this->commodity_attr_id);
        $shop_id = $this->shop_id;

        $com_lists = CommodityAttr::whereIn('id', $attr_id)->with('commodity')->get();
        $com_lists->transform(function ($item) use ($shop_id) {
            $item->commodity_attr_id = $item->id;
            $item->com_img = config('app.img_url') . ($item->commodity->pictures)[0];
            $item->com_name = $item->commodity->commodity_name;
            $item->com_desc = $item->commodity->commodity_desc;
            $item->com_price = ShopCom::where(['shop_id' => $shop_id, 'commodity_attr_id' => $item->id, 'is_buy' => 1])->value('price');
            $item->attr = $item->attr_value;
            return $item->only(['commodity_attr_id','com_img', 'com_name', 'com_desc', 'com_price', 'attr']);
        });

        switch ($this->status) {
            case '11': 
                $status = '退货中';
                break;
            case '12': 
                $status = '审核中';
                break;
            case '13': 
                $status = '已退款';
                break;

            default:
                $status = '未知状态';
                break;
        }

        return [
            'id' => $this->id,
            'rent_id' => $this->rent_id,
            'shop_id' => $this->shop_id,
            'com_lists' => $com_lists,
            'code' => $this->status,
            'status' => $status,
            'comment' => $this->comment,
        ];

    }

}
